// pages/profile/index.ts
Page({
  data:{
nickName:'',
avatar:''
  },
  onLoad(){
    const app = getApp()
    console.log(getApp());
    this.setData({
      ...app.userProfile
    })
    // this.setData({
    //   nickName:app.nickName,
    //   avatar:app.avatar
    // })
  },
  getUserNickname(ev){
    // console.log(ev.detail.value);
    this.updateNickname(ev.detail.value)
  },
  //保存昵称接口
  async updateNickname(nickName){
    if(nickName === '') return
    this.setData({nickName})
 const {code} = await wx.http.put('/userInfo',{nickName})
 if(code!==10000) return wx.utils.toast('更新昵称失败')
  },
  //触发头像事件
  getUserAvatar(ev){
    console.log(ev.detail.avatarUrl);
 this.updateAvatar(ev.detail.avatarUrl)
  },
  //上传头像接口
  updateAvatar(avatar){
    wx.uploadFile({
      filePath: avatar,
      name: 'file',
      url: wx.http.baseURL+'/upload',
      header:{  Authorization: 'Bearer ' + getApp().token},
      formData:{type:'avatar'},
      success:(res)=>{
        // console.log(res.data);
        const data = JSON.parse(res.data)
        if(data.code!==10000) return wx.utils.toast('头像上传失败')
        // console.log(data);
        getApp().avatar = avatar
        this.setData({avatar})
      }
    })
  }
})